public class testwork1 {
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int rleft=ksum(root.left);
        int rright=ksum(root.right);
        int temp=Math.abs(rleft-rright);
        if(temp>1){
            return false;
        }
        return isBalanced(root.left)&&isBalanced(root.right);//遍历每个结点
    }
    public int ksum(TreeNode root){
        if(root==null){
            return 0;
        }
        int kleft=ksum(root.left);
        int krigth=ksum(root.right);
        return Math.max(kleft,krigth)+1;
    }
}
